package com.cy.dda.service.goodsdiligencing.impl;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingReturnVisitReportAttachmentMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingReturnVisitReportAuditRecordMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingReturnVisitReportMapper;
import com.cy.dda.dao.mapper.user.UserMapper;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.goodsdiligencing.GoodsDiligencingReportAuditAO;
import com.cy.dda.model.ao.goodsdiligencing.ReturnVisitReportSaveAO;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencing;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingReturnVisitReport;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingReturnVisitReportAttachment;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingReturnVisitReportAuditRecord;
import com.cy.dda.model.domain.user.User;
import com.cy.dda.model.enums.diligencing.DiligencingReportAuditResult;
import com.cy.dda.model.enums.diligencing.DiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingAttachmentType;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingType;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAttachmentVO;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAuditRecordVO;
import com.cy.dda.model.vo.goodsdiligencing.ReturnVisitReportAdminDetailVO;
import com.cy.dda.model.vo.goodsdiligencing.ReturnVisitReportDetailVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.goodsdiligencing.ReturnVisitReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description 回访报告 接口实现类
 * @Author tanyilin
 * @Date 2019/8/28 16:36
 */
@Service
public class ReturnVisitReportServiceImpl extends BaseService implements ReturnVisitReportService {

    @Autowired
    private GoodsDiligencingMapper                             goodsDiligencingMapper;
    @Autowired
    private GoodsDiligencingReturnVisitReportMapper            returnVisitReportMapper;
    @Autowired
    private GoodsDiligencingReturnVisitReportAttachmentMapper  returnVisitReportAttachmentMapper;
    @Autowired
    private GoodsDiligencingReturnVisitReportAuditRecordMapper returnVisitReportAuditRecordMapper;
    @Autowired
    private UserMapper                                         userMapper;


    @Override
    public ReturnVisitReportDetailVO getReturnVisitReportDetail(Long goodsDiligencingId) {

        GoodsDiligencingReturnVisitReport returnVisitReport = new GoodsDiligencingReturnVisitReport();
        returnVisitReport.setGoodsDiligencingId(goodsDiligencingId);
        returnVisitReport = returnVisitReportMapper.selectOne(returnVisitReport);
        if(returnVisitReport != null) {
            //回访报告已填写
            ReturnVisitReportDetailVO detailVO = new ReturnVisitReportDetailVO();
            BeanUtils.copyProperties(returnVisitReport, detailVO);
            detailVO.setReturnVisitReportId(returnVisitReport.getId());

            //查询家具照片图片
            List<GoodsDiligencingReportAttachmentVO> furnitureImages = returnVisitReportAttachmentMapper.selectReturnVisitReportAttachment(returnVisitReport.getId(), GoodsDiligencingAttachmentType.FURNITURE_IMAGE);
            //查询现场在场图片
            List<GoodsDiligencingReportAttachmentVO> sceneImages = returnVisitReportAttachmentMapper.selectReturnVisitReportAttachment(returnVisitReport.getId(), GoodsDiligencingAttachmentType.SCENE_IMAGE);

            detailVO.setFurnitureImages(furnitureImages);
            detailVO.setSceneImages(sceneImages);

            return detailVO;
        }

        return null;
    }

    @Override
    @Transactional
    public Result<Map<String, Object>> saveReturnVisitReport(ReturnVisitReportSaveAO saveAO, Long userId) {

        Date time = new Date();
        Long goodsDiligencingId = saveAO.getGoodsDiligencingId();
        Long returnVisitReportId = saveAO.getReturnVisitReportId();

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        if(!(GoodsDiligencingType.RETURN_VISIT_INVESTIGATE.equals(dbGoodsDiligencing.getDiligencingType())
                //&& DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus())
                && GoodsDiligencingStatus.RETURN_VISIT_ING.equals(dbGoodsDiligencing.getStatus()))) {
            return Result.fail("当前尽调阶段不可提交回访调查报告");
        }

        if(CommonUtils.isEmpty(returnVisitReportId)) {
            //回访报告ID为空，则说明还未提交过回访报告，则新建一个回访报告
            GoodsDiligencingReturnVisitReport newReturnVisitReport = new GoodsDiligencingReturnVisitReport();
            BeanUtils.copyProperties(saveAO, newReturnVisitReport);
            newReturnVisitReport.setGoodsDiligencingId(goodsDiligencingId);
            newReturnVisitReport.setUserId(userId);
            newReturnVisitReport.setCreateTime(time);
            newReturnVisitReport.setUpdateTime(time);
            returnVisitReportMapper.insertSelective(newReturnVisitReport);
            returnVisitReportId = newReturnVisitReport.getId();
        }else {
            //回访报告ID不为空，说明回访报告已存在，只需更新
            GoodsDiligencingReturnVisitReport updateReturnVisitReport = new GoodsDiligencingReturnVisitReport();
            BeanUtils.copyProperties(saveAO, updateReturnVisitReport);
            updateReturnVisitReport.setId(returnVisitReportId);
            updateReturnVisitReport.setUserId(userId);
            updateReturnVisitReport.setUpdateTime(time);
            returnVisitReportMapper.updateByPrimaryKeySelective(updateReturnVisitReport);
        }

        //保存家具照片图片
        List<GoodsDiligencingReportAttachmentVO> furnitureImages = saveAO.getFurnitureImages();
        if(!CommonUtils.isEmpty(furnitureImages)) {
            List<GoodsDiligencingReturnVisitReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentVO bean : furnitureImages) {
                GoodsDiligencingReturnVisitReportAttachment reportAttachment = new GoodsDiligencingReturnVisitReportAttachment();
                BeanUtils.copyProperties(bean, reportAttachment);
                reportAttachment.setReturnVisitReportId(returnVisitReportId);
                reportAttachment.setCreateTime(time);
                reportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(reportAttachment);
            }
            //批量新增处理
            returnVisitReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            returnVisitReportAttachmentMapper.deleteByUpdateTimeAndType(returnVisitReportId, GoodsDiligencingAttachmentType.FURNITURE_IMAGE, time);
        }

        //保存现场在场图片
        List<GoodsDiligencingReportAttachmentVO> sceneImages = saveAO.getSceneImages();
        if(!CommonUtils.isEmpty(sceneImages)) {
            List<GoodsDiligencingReturnVisitReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentVO bean : sceneImages) {
                GoodsDiligencingReturnVisitReportAttachment reportAttachment = new GoodsDiligencingReturnVisitReportAttachment();
                BeanUtils.copyProperties(bean, reportAttachment);
                reportAttachment.setReturnVisitReportId(returnVisitReportId);
                reportAttachment.setCreateTime(time);
                reportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(reportAttachment);
            }
            //批量新增处理
            returnVisitReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            returnVisitReportAttachmentMapper.deleteByUpdateTimeAndType(returnVisitReportId, GoodsDiligencingAttachmentType.SCENE_IMAGE, time);
        }

        //更改尽调报告状态为‘待确认’
        if(DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus()) || DiligencingStatus.REPORT_RETURN.equals(dbGoodsDiligencing.getDiligencingStatus())) {
            GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
            updateGoodsDiligencing.setId(dbGoodsDiligencing.getId());
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_CONFIRM_WAIT);
            updateGoodsDiligencing.setConclusion(saveAO.getConclusion());
            updateGoodsDiligencing.setUpdateTime(time);
            goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("goodsDiligencingId", goodsDiligencingId);
        map.put("returnVisitReportId", returnVisitReportId);

        return Result.success("保存成功", map);
    }

    @Override
    @Transactional
    public void doAuditReturnVisitReport(GoodsDiligencingReportAuditAO auditAO, Long managerId) {

        Date time = new Date();
        Long goodsDiligencingId = auditAO.getGoodsDiligencingId();

        //先查询在库的 回访报告
        GoodsDiligencingReturnVisitReport returnVisitReport = new GoodsDiligencingReturnVisitReport();
        returnVisitReport.setGoodsDiligencingId(goodsDiligencingId);
        returnVisitReport = returnVisitReportMapper.selectOne(returnVisitReport);
        state(returnVisitReport != null, "数据异常，该尽调报告不存在");

        //添加审核记录
        GoodsDiligencingReturnVisitReportAuditRecord auditRecord = new GoodsDiligencingReturnVisitReportAuditRecord();
        auditRecord.setReturnVisitReportId(returnVisitReport.getId());
        auditRecord.setResult(auditAO.getResult());
        auditRecord.setRemark(auditAO.getRemark());
        auditRecord.setManagerId(managerId);
        auditRecord.setCreateTime(time);
        auditRecord.setUpdateTime(time);
        returnVisitReportAuditRecordMapper.insertSelective(auditRecord);

        //更改 家具尽调 的状态
        GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
        updateGoodsDiligencing.setId(goodsDiligencingId);
        if(DiligencingReportAuditResult.PASS.equals(auditAO.getResult())) {
            //通过，说明 已回访
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.RETURN_VISIT_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.COMPLETED);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.ALREADY_RETURN_VISIT);
        }else if(DiligencingReportAuditResult.RETURN.equals(auditAO.getResult())) {
            //退回，说明还是 回访中
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.RETURN_VISIT_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_RETURN);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.RETURN_VISIT_ING);
        }
        updateGoodsDiligencing.setUpdateTime(time);
        goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);

    }

    @Override
    public ReturnVisitReportAdminDetailVO getReturnVisitReportAdminDetail(Long goodsDiligencingId) {

        //先查询在库的 回访报告
        GoodsDiligencingReturnVisitReport returnVisitReport = new GoodsDiligencingReturnVisitReport();
        returnVisitReport.setGoodsDiligencingId(goodsDiligencingId);
        returnVisitReport = returnVisitReportMapper.selectOne(returnVisitReport);
        if(returnVisitReport != null) {
            //回访报告已存在
            ReturnVisitReportAdminDetailVO detailVO = new ReturnVisitReportAdminDetailVO();
            BeanUtils.copyProperties(returnVisitReport, detailVO);

            //查找调查员
            User user = userMapper.selectByPrimaryKey(returnVisitReport.getUserId());
            detailVO.setInvestigatorName(user.getName());

            //查询报告审核记录
            List<GoodsDiligencingReportAuditRecordVO> reportAuditRecordList = returnVisitReportAuditRecordMapper.selectReturnVisitReportAuditRecord(returnVisitReport.getId());
            detailVO.setReportAuditRecordList(reportAuditRecordList);

            //查询报告图片
            List<GoodsDiligencingReportAttachmentVO> reportAttachmentList = returnVisitReportAttachmentMapper.selectReturnVisitReportAttachment(returnVisitReport.getId(), null);
            detailVO.setReportAttachmentList(reportAttachmentList);

            return detailVO;

        }

        return new ReturnVisitReportAdminDetailVO();
    }


}
